<template>
  <div class="">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-button type="primary" style="margin-bottom: 15px" @click="showAddCate">添加分类</el-button>
      <tree-table class="treeTable" :data="categorys" :columns="columns" :selection-type="false" :expand-type="false" show-index index-text="#" border>
        <template slot="isOK" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false"></i>
          <i class="el-icon-error" v-else></i>
        </template>
        <template slot="sort" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" type="success">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <template slot="operation" slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="暂未开发" placement="top" :enterable="false">
            <el-button type="primary" icon="el-icon-edit" size="small">编辑</el-button>
          </el-tooltip>
          <el-button type="danger" icon="el-icon-delete" size="small" @click="deleteCateById(scope.row.cat_id)">删除</el-button>
        </template>
      </tree-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 3, 5]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>

      <el-dialog title="添加分类" :visible.sync="DialogVisible" width="35%" @close="clear">
        <el-form :model="addCateForm" :rules="rules" ref="addCate" label-width="100px">
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addCateForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader v-model="addCateValue" :props="cateProps" :options="parentCateList" @change="handleChange" clearable change-on-select></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="DialogVisible=false">取 消</el-button>
          <el-button type="primary" @click="addCategory">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      categorys: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOK'
        },
        {
          label: '排序',
          type: 'template',
          template: 'sort'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operation'
        }
      ],
      total: 0,
      DialogVisible: false,
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      rules: {
        cat_name: [{ required: true, trigger: 'blur', message: '请输入分类的名称' }]
      },
      parentCateList: [],
      cateProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      addCateValue: []
    }
  },
  methods: {
    async getCategorys () {
      const { data } = await this.$http.get('/categories', { params: this.queryInfo })
      if (data.meta.status === 200) {
        this.categorys = data.data.result
        this.total = data.data.total
      } else {
        this.$message.error('获取商品分类信息失败')
      }
    },
    async showAddCate () {
      this.DialogVisible = !this.DialogVisible
      const { data } = await this.$http.get('/categories', { params: { type: 2 } })
      if (data.meta.status !== 200) return this.$message.error(`${data.meta.msg}`)
      this.parentCateList = data.data
    },
    addCategory () {
      this.$refs.addCate.validate(async valid => {
        if (valid) {
          const { data } = await this.$http.post('categories', this.addCateForm)
          if (data.meta.status !== 201) return this.$message.error(`${data.meta.msg}`)
          this.$message.success(`${data.meta.msg}`)
          this.DialogVisible = false
          this.getCategorys()
        }
      })
    },
    deleteCateById (id) {
      this.$confirm('是否永久删除该次分类', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data } = await this.$http.delete(`categories/${id}`)
        if (data.meta.status !== 200) return this.$message.error(`${data.meta.msg}`)
        this.$message.success('删除成功!')
        this.getCategorys()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    clear () {
      this.$refs.addCate.resetFields()
      this.addCateValue = []
      this.addCateForm.cat_id = 0
      this.addCateForm.cat_level = 0
    },
    handleChange () {
      // this.addCateValue长度0，就是创建一级分类
      // 大于0，不是一级分类
      if (this.addCateValue.length > 0) {
        this.addCateForm.cat_pid = this.addCateValue[this.addCateValue.length - 1]
        this.addCateForm.cat_level = this.addCateValue.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_plevel = 0
      }
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getCategorys()
    },
    handleCurrentChange (newCurrent) {
      this.queryInfo.pagenum = newCurrent
      this.getCategorys()
    }
  },
  created () {
    this.getCategorys()
  }
}
</script>

<style lang="scss" scoped>
i {
  font-size: 16px;
}
.el-cascader-panel {
  height: 150px;
}
.el-popper .el-cascader__dropdown {
  height: 150px;
}
</style>
